package spreedsheet;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by joker on 2017/8/16.
 */
public class SpreadSheet {
    public static void main(String[] args) {
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(SpreadSheet.add(1,"joker",new BigDecimal(60000)));
        employeeList.add(SpreadSheet.add(2,"faker",new BigDecimal(60001)));
        employeeList.add(SpreadSheet.add(3,"mike",new BigDecimal(60002)));
        employeeList.add(SpreadSheet.add(4,"jone",new BigDecimal(60003)));
        /**
         * 创建新的空白工作簿
         */
        XSSFWorkbook workbook = new XSSFWorkbook();
        /**
         * 创建sheet
         */

        XSSFSheet sheet = workbook.createSheet("Employee info");
        String[] objects = {"ID","Name","Bonus"};
        XSSFRow row;
        int rowid = 0;
        row = sheet.createRow(rowid++);
        int cellid = 0;
        //标题文件
        for (int i = 0;i < objects.length;i++){
            XSSFCell  cell = row.createCell(cellid++);
            cell.setCellValue(objects[i]);
        }
        //插入元素
        for (int i = 0; i < employeeList.size(); i++) {
            row = sheet.createRow(rowid++);
            int celli = 0;
            XSSFCell  cell = row.createCell(celli++);
            cell.setCellValue(employeeList.get(i).getId()+"");
            cell = row.createCell(celli++);
            cell.setCellValue(employeeList.get(i).getName());
            cell = row.createCell(celli++);
            cell.setCellValue(employeeList.get(i).getBonus().toString());
        }

        try {
            FileOutputStream outputStream = new FileOutputStream(new File("D:/sheet.xls"));
            try {
                workbook.write(outputStream);
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Employee add(Integer id, String name, BigDecimal bonus){
        Employee employee = new Employee();
        employee.setId(id);
        employee.setBonus(bonus);
        employee.setName(name);
        return employee;
    }
}
